
package com.funo.government.info.components;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.widget.RemoteViews;

import com.funo.government.info.R;
import com.funo.government.info.bean.PushInfo;
import com.funo.government.info.constant.Constant;
import com.funo.government.info.utils.BusinessException;
import com.funo.government.info.utils.BusinessResolver.BusinessCallback;
import com.funo.government.info.utils.GovernmentResolver;
import com.funo.government.info.view.MainActivity;

public class QueryMessageReceiver extends BroadcastReceiver {

    private NotificationManager notificationManager;

    private static final int ID = 123435;

    protected static final String TAG = "QueryMessageReceiver";

    private SharedPreferences mPreferences;

    @Override
    public void onReceive(final Context context, Intent intent) {

        if (notificationManager == null) {
            notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
        }
        if (mPreferences == null) {
            mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        }
        if (!mPreferences.getBoolean(Constant.PRE_KEY_PUSH_SETTINGS, true)) {
            return;
        }
        String areaId = mPreferences.getString(Constant.PRE_KEY_AREA_ID,
                context.getString(R.string.default_area_id));
        GovernmentResolver.getPush(areaId, new BusinessCallback<PushInfo>() {

            @Override
            public void onSuccess(PushInfo pushInfo) {
                if (pushInfo != null) {
                    String pushId = mPreferences.getString(Constant.PRE_KEY_PUSH_ID, null);
                    if (!pushInfo.noticeId.equals(pushId)) {
                        notifyMessage(context, pushInfo);
                        mPreferences.edit().putString(Constant.PRE_KEY_PUSH_ID, pushInfo.noticeId)
                                .commit();
                    }
                }
            }

            @Override
            public void onError(BusinessException e) {
                e.printStackTrace();
            }
        });

    }

    /**
     * 发送通知
     * 
     * @param context
     * @param recentMessage
     */
    private void notifyMessage(Context context, PushInfo recentMessage) {
        if (notificationManager != null) {
            Intent intent;
            RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
            Notification notification = new Notification();
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notification.contentView = rv;
            notification.icon = R.drawable.ic_launcher;

            rv.setTextColor(R.id.tvTitle, Color.BLACK);
            rv.setTextColor(R.id.tvContent, Color.BLACK);
            rv.setImageViewResource(R.id.ivLogo, R.drawable.ic_launcher);
            rv.setTextViewText(R.id.tvTitle, recentMessage.title);
            rv.setTextViewText(R.id.tvContent, recentMessage.content);
            intent = new Intent(context, MainActivity.class);
            notification.contentIntent = PendingIntent.getActivity(context, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            int notifiDefault = Notification.DEFAULT_LIGHTS;
            notification.defaults = notifiDefault;
            notificationManager.notify(ID, notification);
        }

    }
}
